<?php

require_once 'constants.php';

abstract class Buffalo_Post_Type {

    private $post_type;

    function __construct($post_type) {
        $this->post_type = $post_type;
        add_action('init', array(&$this, 'register_post_type'));
        add_action('manage_' . $this->post_type . '_posts_custom_column', array(&$this, 'manage_column_outputs'));
        add_filter('manage_edit-' . $this->post_type . '_columns', array(&$this, 'change_column_names'));
    }

    function register_post_type() {
        $args = $this->get_args();
        register_post_type($this->post_type, $args);
    }

    function manage_column_outputs($column_name) {
        switch ($column_name) {
            default:
                break;
        }
    }

    function change_column_names($columns) {
        return $columns;
    }

    protected abstract function get_args();

    protected function get_labels($singular, $plural='') {
        if (empty($plural)) {
            $plural = $singular . 's';
        }

        $labels = array(
            'name' => _x($plural, 'post type general name'),
            'singular_name' => _x($singular, 'post type singular name'),
            'add_new' => __('Add New'),
            'all_items' => __('All ' . $plural),
            'add_new_item' => __('Add New ' . $singular),
            'edit_item' => __('Edit ' . $singular),
            'new_item' => __('New ' . $singular),
            'view_item' => __('View ' . $singular),
            'search_items' => __('Search ' . $plural),
            'not_found' => __('No ' . strtolower($plural) . ' found.'),
            'not_found_in_trash' => __('No ' . strtolower($plural) . ' found in Trash.')
        );

        return $labels;
    }

}

class Product_Post_Type extends Buffalo_Post_Type {

    function __construct() {
        parent::__construct(PRODUCT_POST_TYPE);
    }

    function change_column_names($columns) {
        $new_columns['cb'] = '<input type="checkbox" />';
        $new_columns['title'] = __('Name');
        $new_columns['cats'] = __('Categories');
        $new_columns['price'] = __('Price');
        return $new_columns;
    }

    function manage_column_outputs($column_name) {
        global $post;

        switch ($column_name) {
            case PRICE_META_KEY:
                echo '$' . get_post_meta($post->ID, PRICE_META_KEY, true);
                break;
            case 'cats':
                $cat = wp_get_post_terms($post->ID, CATEGORY_TAXONOMY);
                $count = count($cat);
                for ($i = 0; $i < $count; $i++) {
                    echo $cat[$i]->name;
                    if ($i != $count - 1) {
                        echo ', ';
                    }
                }
                break;
            default:
                break;
        }
    }

    protected function get_args() {
        return array(
            'labels' => $this->get_labels('Product'),
            'public' => true,
            'show_in_menu' => true,
            'supports' => array('title', 'editor', 'thumbnail')
        );
    }

}

class Order_Post_Type extends Buffalo_Post_Type {

    public function __construct() {
        parent::__construct(ORDER_POST_TYPE);
    }

    public function change_column_names($columns) {
        $new_columns['cb'] = '<input type="checkbox" />';
        $new_columns['title'] = __('Name');
        $new_columns['date'] = __('Date');
        $new_columns['status'] = __('Status');
        return $new_columns;
    }

    public function manage_column_outputs($column_name) {
        global $post;
        switch ($column_name) {
            case 'status':
                echo get_post_meta($post->ID, 'buffalo-status', true);
                break;
            default:
                break;
        }
    }

    protected function get_args() {
        return array(
            'labels' => $this->get_labels('Order'),
            'public' => true,
            'show_in_menu' => true,
            'supports' => array('title')
        );
    }

}

?>
